﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
using System.Windows.Forms;
using CookComputing.XmlRpc;

namespace subdump
{
    public class OpenSubtitles
    {
        
        public OpenSubtitles()
        {
            Results = new ArrayList();
        }
        public ArrayList Results
        {
            get; set;
        }
        

        
        public void loadOpenSubtitlesInfo()
        {
            IOpenSubtitle openSubtitle = XmlRpcProxyGen.Create<IOpenSubtitle>();
            XmlRpcStruct mystruct;
            try
            {
                mystruct = openSubtitle.serverInfo();
                foreach (DictionaryEntry d in mystruct)
                {
                    Info = Info + d.Key + " : " + d.Value + "\n";
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error contacting server!");
            }
        }
        public void searchMovie(String path)
        {
            MovieHash mh = new MovieHash(path);
            //MessageBox.Show(mh.Hash);
            Console.WriteLine(mh.Hash);
            IOpenSubtitle openSubtitle = XmlRpcProxyGen.Create<IOpenSubtitle>();
            XmlRpcStruct mystruct;
            XmlRpcStruct first = new XmlRpcStruct();
            first.Add("sublanguageid", "");
            first.Add("moviehash", mh.Hash);
            FileInfo fi = new FileInfo(path);
            first.Add("moviebytesize",(System.Int32)fi.Length);
            first.Add("imdbid", "");
            
            
            
            XmlRpcStruct [] rij = new XmlRpcStruct[1];
            rij[0] = first;
            try
            {
                mystruct = openSubtitle.searchSubtitles("00000", rij);
                foreach (DictionaryEntry d in mystruct)
                {
                    Info = Info + d.Key + " : " + d.Value + d.GetType() + "\n";
                    Console.WriteLine("voor de if");
                    if (d.Value is System.Object[])
                    {
                      Object[] b = (Object[])d.Value;
                        foreach (XmlRpcStruct r in b)
                        {
                            OpenSubtitlesResult osResult = new OpenSubtitlesResult();
                            foreach (DictionaryEntry p in r)
                            {
                                Info = Info + "\t" + p.Key + " : " + p.Value + "\n";
                                osResult.setVar(p.Key.ToString(),p.Value.ToString());
                            }
                            Results.Add(osResult);
                        }
                      //Console.WriteLine("dit is het"+b.Length+" "+b[0].GetType());



                    }
                }
                //MessageBox.Show(Info);
                Console.WriteLine(Info);

            }
            catch (Exception)
            {
                //MessageBox.Show("Error contacting server!");
                Console.WriteLine("error");
            }
        }
        public void checkMovieHash(String path)
        {
            MovieHash mh = new MovieHash(path);
            Console.WriteLine(mh.Hash);

            IOpenSubtitle openSubtitle = XmlRpcProxyGen.Create<IOpenSubtitle>();
            XmlRpcStruct mystruct;
            String [] hashes = new string[1];
            hashes[0] = mh.Hash;
            try
            {
                mystruct = openSubtitle.checkMovieHash("00000", hashes);
                foreach (DictionaryEntry d in mystruct)
                {
                    Info = Info + d.Key + " : " + d.Value + "\n";
                    if (d.Value is XmlRpcStruct)
                    {
                        
                            XmlRpcStruct res = (XmlRpcStruct) d.Value;
                        
                        
                        
                        foreach (DictionaryEntry e in res)
                        {
                            Info = Info + "\t" + e.Key + " : " + e.Value +  "\n";
                            if(e.Value is XmlRpcStruct)
                            {

                                XmlRpcStruct final = (XmlRpcStruct) e.Value;
                                foreach(DictionaryEntry f in final)
                                {
                                    Info = Info + "\t\t" + f.Key + ":" + f.Value + "\n";
                                }
                                

                            }
                       
                        }
                    }
                }
                //MessageBox.Show(Info);
                Console.WriteLine(Info);

            }
            catch (Exception e)
            {
                //MessageBox.Show("Error contacting server!");
                Console.WriteLine(e.ToString());
            }
        }

       
        

        public string Info { get; set; }
    }
}